package com.medical.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/*
*
* 登陆验证工具
*
* */
@Component
public class JwtUtil {
    public static final String SECRET_KEY = "546L5qKm6L+q"; // 密钥 Base64加密后的密钥
    public static final long EXPIRATION_TIME = 3600*1000*12; // 12小时
    
    private static final String TOKEN_BLACKLIST_PREFIX = "token:blacklist:";
    


    public static String generateToken(Map<String, Object> claims) {
        try {
            System.out.println("JwtUtil: 开始生成Token, claims: " + claims);
            String token = Jwts.builder()
                    .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                    .addClaims(claims)
                    .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
                    .compact();
            System.out.println("JwtUtil: Token生成成功");
            return token;
        } catch (Exception e) {
            System.err.println("JwtUtil生成Token异常: " + e.getClass().getName() + ": " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

    public static Claims parseToken(String token) throws Exception {
        try {
            return Jwts.parser()
                    .setSigningKey(SECRET_KEY)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            System.err.println("JwtUtil解析Token异常: " + e.getClass().getName() + ": " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }
    

}
